<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/16
 * Time: 10:53
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserBase extends StModel
{

    protected $table = 'tn_user_base';

    protected $fillable = [
        'name', 'phone', 'sex', 'password', 'hospital_id', 'department', 'province', 'city', 'county', 'nation','type',
        'birthday', 'show_mobile', 'invite_code', 'invite_original_code', 'invite_type', 'account', 'rong_token','created_at'
    ];


    public function isAdmin()
    {
        return $this->type == 0;
    }

    public function isPatient()
    {
        return $this->type == 1;
    }

    public function isDoctor()
    {
        return $this->type == 2;
    }

    public function isDoctorsAdmin()
    {
        return $this->type == 3;
    }

    public function isRegionAdmin()
    {
        return $this->type == 4;
    }

    public function isDelAdmin()
    {
        $result = 0;
        $admin_ids = config('admin.admin_ids');

        if (in_array($this->id, $admin_ids)) {
            $result = 1;
        }

        return $result;
    }

    public function detail()
    {
        return $this->hasOne('App\Models\DoctorDetail', 'ACCOUNT', 'account');
    }

    /**
     * 获取与用户关联的用户信息
     */
    public function patient()
    {
        return $this->hasOne('App\Models\PatientDetail', 'ACCOUNT', 'account');
    }

    /**
     * 获取与用户关联的管理医院数据
     */
    public function hospitals()
    {
        return $this->hasOne('App\Models\SceneManage', 'account', 'account');
    }
    /* tds create
     * @param $userId int
     * @return hospitalId int
     **/
    public function getHospitalIdByUserId($userId)
    {
        $userInfo = $this->select('hospital_id')
            ->where('id', $userId)->first()->toArray();

        if (!empty($userInfo)) {
            return $userInfo['hospital_id'];
        }

        return -1;
    }
}